import { FileModel } from '@/types/fileModel';
import config from './config';
import { isEmpty, isNull } from './is';
import { HideLoading, Loading, Toast } from './uniapi/prompt';
import { useAuthStore } from '@/store/modules/auth';
import { ResultEnum } from '@/enums/httpEnum';
import { getEnvValue } from '@/utils/env';
import { Base64 } from 'js-base64';
import { router } from '@/utils/router';

const BASE_URL = getEnvValue<string>('VITE_PUBLIC_PATH');
const VITE_PROXY_BASE_URL = getEnvValue<string>('VITE_PROXY_BASE_URL');

/**
 * 上传文件
 * @param file 文件对象
 */
export function uploadImage(file: FileModel, isImg: boolean, callback?: Function) {
  if (isNull(file)) {
    Toast('文件为空');
    return;
  }
  const authStore = useAuthStore();

  Loading('上传中');
  uni.uploadFile({
    url: BASE_URL + config.url.uploadFile,
    header: {
      token: authStore.getToken,
    },
    formData: { isImg: isImg },
    filePath: file.url,
    name: 'file',
    success: (uploadFileRes) => {
      HideLoading();
      if (uploadFileRes.statusCode == ResultEnum.SUCCESS) {
        if (typeof callback === 'function') {
          // 数据加载完成回调函数
          callback(uploadFileRes.data);
        }
      }
    },
    fail: () => {
      Toast('上传失败！');
    },
    complete: function () {
      HideLoading();
    },
  });
}

/**
 * 批量上传
 * @param files 文件对象
 */
export function uploadImages(
  files: Array<UniApp.UploadFileOptionFiles>,
  isImg: boolean,
  callback?: Function,
) {
  if (isEmpty(files)) {
    Toast('文件为空');
    return;
  }

  const authStore = useAuthStore();

  Loading('上传中');

  uni.uploadFile({
    url: BASE_URL + config.url.uploadFile,
    header: {
      token: authStore.getToken,
    },
    formData: { isImg: isImg },
    files: files,
    success: (uploadFileRes) => {
      HideLoading();
      if (uploadFileRes.statusCode == ResultEnum.SUCCESS) {
        if (typeof callback === 'function') {
          // 数据加载完成回调函数
          callback(uploadFileRes.data);
        }
      }
    },
    fail: () => {
      Toast('上传失败！');
    },
    complete: function () {
      HideLoading();
    },
  });
}

export function uploadFiles(file: any, isImg: boolean, callback?: Function) {
  const authStore = useAuthStore();
  uni.uploadFile({
    url: BASE_URL + config.url.uploadFile,
    header: {
      token: authStore.getToken,
    },
    formData: { isImg: isImg },
    files: [file],
    success: (uploadFileRes) => {
      HideLoading();
      if (uploadFileRes.statusCode == 200) {
        if (typeof callback === 'function') {
          // 数据加载完成回调函数
          callback(JSON.parse(uploadFileRes.data));
        }
      }
    },
    fail: () => {
      Toast('上传失败！');
    },
    complete: function () {
      HideLoading();
    },
  });
}

/**
 * 下载
 * @param url 路径
 */
export function downloadFile(url: string) {
  if (isEmpty(url)) {
    Toast('下载链接无效！');
  }
  Loading('下载中');
  const reg = /^(http:|https:).*/gi;
  const downloadUrl = reg.test(url) ? url : BASE_URL + url;
  const authStore = useAuthStore();

  uni.downloadFile({
    url: downloadUrl,
    header: {
      token: authStore.getToken,
    },
    success: (res) => {
      if (res.statusCode === 200) {
        Toast('下载成功');
      }
    },
    fail: () => {
      Toast('上传失败！');
    },
    complete: function () {
      HideLoading();
    },
  });
}

export function previewFile(url: string) {
  if (isEmpty(url)) {
    Toast('链接无效！');
  }
  router.push(
    '/pages/process/file-preview?url=' +
      encodeURIComponent(Base64.encode(VITE_PROXY_BASE_URL + BASE_URL + url)),
  );
}
